# 项目是获取句子返回结果存储为数据的
# 导入必须的包
import time
import requests #发起网络请求的
import json #进行格式解析

# 1.定义url

url = "https://v.api.aa1.cn/api/api-wenan-qg/index.php?aa1=json"

response = requests.get(url)
# html = response.text
# print(html)
"""
[{
    "qinggan":"离别之后，每一个时刻，每一句话，都是最珍贵的回忆与执念。",
    "date":"2025-10-22",
    "code"：200
}]
"""

# 截取字符串写法1,完成
# print(html[14:-3])


# try:
#
# except Exception as e:
#     print(e)


# 第一，time.sleep(s)

# try:
#     data = response.json()
#     if isinstance(data, list):
#         sentence = data[0]["qinggan"]
#         print("提取到的句子：", sentence)
#     else:
#         print("返回格式不符合预期")
# except json.JSONDecodeError:
#     print("返回内容不是有效 JSON")


# try:
#     data = '[{"qinggan":"离别之后，每一个时刻，每一句话，都是最珍贵的回忆与执念。","date":"2025-10-22","code"："200"}]'
#     parsed = json.loads(data)
#     if isinstance(parsed, list):
#         sentence = parsed[0]["date"]
#         print("date:", sentence)
#     else:
#         print("else")
# except json.JSONDecodeError:
#     print("JSON 解析错误")

def get_code():

    try:
        data = '[{"qinggan":"离别之后，每一个时刻，每一句话，都是最珍贵的回忆与执念。","date":"2025-10-22","code":"200"}]'
        parsed = json.loads(data) #需要loads一下,response.json
        if isinstance(parsed, list):
        # if isinstance(parsed, list) and len(parsed) > 0 and "date" in parsed[0]:
            sentence = parsed[0]["code"]
            print("code:", sentence)
        else:
            print("else")
    except json.JSONDecodeError:
        print("JSON 解析错误")
#定时器1
def simple_task():
    print("任务执行时间：", time.ctime())


if __name__ == '__main__':
    get_code()
    print("5s后,重新运行")
    time.sleep(5)
    print("5s到时,正在重新运行.....")
    get_code()

# 每隔5分钟执行一次任务
    while True:
        simple_task()
        time.sleep(5 * 60)